home *** CD-ROM | disk | FTP | other *** search
/ Programmer Plus 2007 / Programmer-Plus-2007.iso / Programming / Report Writers / Crystal Repot 9.0 Full CD version / Setup.exe / SRC / HOARDDLL.ZIP / 3rdParty / hoard / libhoard-2.0.2 / mergelogs.cpp < prev    next >
Encoding:
C/C++ Source or Header  |  2000-04-01  |  4.2 KB  |  170 lines

  1. ///-*-C++-*-//////////////////////////////////////////////////////////////////
  2. //
  3. // Hoard: A Fast, Scalable, and Memory-Efficient Allocator
  4. //        for Shared-Memory Multiprocessors
  5. // Contact author: Emery Berger, http://www.cs.utexas.edu/users/emery
  6. //
  7. // Copyright (c) 1998-2000, The University of Texas at Austin.
  8. //
  9. // This library is free software; you can redistribute it and/or modify
  10. // it under the terms of the GNU Library General Public License as
  11. // published by the Free Software Foundation, http://www.fsf.org.
  12. //
  13. // This library is distributed in the hope that it will be useful, but
  14. // WITHOUT ANY WARRANTY; without even the implied warranty of
  15. // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
  16. // Library General Public License for more details.
  17. //
  18. //////////////////////////////////////////////////////////////////////////////
  19.  
  20. #include "log.h"
  21. #include "log2.h"
  22. #include "memstat.h"
  23.  
  24. #include <assert.h>
  25. #include <iostream.h>
  26. #include <unistd.h>
  27. #include <stdlib.h>
  28.  
  29.  
  30. void mergeLogs (Log2<MemoryRequest>& l1,
  31.         Log2<MemoryRequest>& l2,
  32.         Log<MemoryRequest>& outLog)
  33. {
  34.   int i = 0;
  35.   int j = 0;
  36.  
  37.   double currTime = 0.0;
  38.   double prevTime = 0.0;
  39.  
  40. #if 0
  41.   // Sanity checks!
  42.   for (i = 0; i < l1.length(); i++) {
  43.     prevTime = currTime;
  44.     currTime = l1[i].getTime();
  45.     assert (prevTime <= currTime);
  46.   }
  47.   currTime = 0.0;
  48.   for (i = 0; i < l2.length(); i++) {
  49.     prevTime = currTime;
  50.     currTime = l2[i].getTime();
  51.     assert (prevTime <= currTime);
  52.   }
  53. #endif
  54.   
  55.   i = 0;
  56.   j = 0;
  57.   currTime = 0.0;
  58.   prevTime = 0.0;
  59.   while ((i < l1.length()) && (j < l2.length())) {
  60.     double l1time = l1[i].getTime();
  61.     double l2time = l2[j].getTime();
  62.     prevTime = currTime;
  63.     if (l1time < l2time) {
  64.       currTime = l1time;
  65.       assert (prevTime <= currTime);
  66.       outLog.append (l1[i]);
  67.       i++;
  68.     } else if (l1time == l2time) {
  69.       // Given the choice between two operations,
  70.       // put allocations first. This is conservative.
  71.       if ((l1[i].getType() == MemoryRequest::MALLOC_OP)
  72.       || (l1[i].getType() == MemoryRequest::REALLOC_OP)
  73.       || (l1[i].getType() == MemoryRequest::ALLOCATE_OP)) {
  74.     currTime = l1time;
  75.     assert (prevTime <= currTime);
  76.     outLog.append (l1[i]);
  77.     i++;
  78.       } else {
  79.     currTime = l2time;
  80.     assert (prevTime <= currTime);
  81.     outLog.append (l2[j]);
  82.     j++;
  83.       }
  84.  
  85.     } else {
  86.       currTime = l2time;
  87.       assert (prevTime <= currTime);
  88.       outLog.append (l2[j]);
  89.       j++;
  90.     }
  91.   }
  92.  
  93.   if (j >= l2.length()) {
  94.     while (i < l1.length()) {
  95.       prevTime = currTime;
  96.       currTime = l1[i].getTime();
  97.       assert (prevTime <= currTime);
  98.       outLog.append (l1[i]);
  99.       i++;
  100.     }
  101.   } else {
  102.     while (j < l2.length()) {
  103.       prevTime = currTime;
  104.       currTime = l2[j].getTime();
  105.       assert (prevTime <= currTime);
  106.       outLog.append (l2[j]);
  107.       j++;
  108.     }
  109.   }
  110.   assert (outLog.length() == l1.length() + l2.length());
  111. }
  112.  
  113.  
  114. int main (int argc, char *argv[])
  115. {
  116.   Log2<MemoryRequest> l, m;
  117.   Log<MemoryRequest> tmp;
  118.  
  119.   char tmpfname[255];
  120.   sprintf (tmpfname, "memory-log%d", getpid());
  121.  
  122.   int NLOGS = 128;
  123.  
  124.   int start = 0;
  125.   int stop = NLOGS;
  126.  
  127.   if (argc > 2) {
  128.     start = atoi(argv[1]);
  129.     stop = atoi(argv[2]);
  130.   }
  131.  
  132.   printf ("Running from %d to %d\n", start, stop);
  133.  
  134.   for (int i = start; i < stop; i++) {
  135.     printf (".");
  136.     fflush (stdout);
  137.  
  138.     unlink (tmpfname);
  139.     tmp.open (tmpfname);
  140.     char l_filename[255];
  141.     sprintf (l_filename, "log%d", i);
  142.     char m_filename[255];
  143.     sprintf (m_filename, "log%d", i + 1);
  144.     // printf ("merging %s and %s\n", l_filename, m_filename);
  145.     m.open (m_filename, Log<MemoryRequest>::READ_ONLY);
  146.     if (m.length() == 0) {
  147.       m.abort();
  148.       unlink (m_filename);
  149.       rename (l_filename, m_filename);
  150.     } else {
  151.       l.open (l_filename, Log<MemoryRequest>::READ_ONLY);
  152.       if (l.length() == 0) {
  153.     l.abort();
  154.     m.abort();
  155.     // Do nothing.
  156.     // m already contains the merged contents of both files.
  157.       } else {
  158.     mergeLogs (l, m, tmp);
  159.     l.abort();
  160.     m.abort();
  161.     tmp.close();
  162.     unlink (m_filename);
  163.     rename (tmpfname, m_filename);
  164.       }
  165.     }
  166.     unlink (l_filename);
  167.   }
  168.   printf ("Done.\n");
  169. }
  170.